$(function() {
	var $modal = $('#my-modal-loading');
	$("#loading_text").text("初始化中...");
	var url = location.href;
	var imageId1;
	var imageId2;
	var imageId3;
	var index = 1;
	
	var imageServerId1;
	var imageServerId2;
	var imageServerId3;
	
	var query_id = localStorage.getItem("query_id");
	var urlPath = "http://yx.fxtourong.com/V1/Goods/createDynamic" + "?store_id=" + query_id;
	
	$("#cancel_back").click(function(){
		window.history.back(-1);
	});

	$modal.modal();
	$.ajax({
		type: "POST",
		url: "http://yx.fxtourong.com/V1/Wechat/getSignPackage",
		data: {
			token: localStorage.getItem("token"),
			url: url,
			verify: "yin@xiong8_(fx-tourong"
		},
		dataType: "json",
		success: function(data) {
			$modal.modal('close');
			var code = data.code;
			if(code == "1") {
				timestamp = data.data.timestamp;
				nonceStr = data.data.nonceStr;
				signature = data.data.signature;
				wx.config({
					debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
					appId: 'wx7f13802caf31f385', // 必填，公众号的唯一标识
					timestamp: timestamp, // 必填，生成签名的时间戳
					nonceStr: nonceStr, // 必填，生成签名的随机串
					signature: signature, // 必填，签名，见附录1
					jsApiList: ['chooseImage', 'uploadImage', 
						 'downloadImage'
					] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
				});
				wx.ready(function() {
					console.log("aaaaaaaaaaaaaaaa");
					$("#openImage").click(function() {
//						wx.checkJsApi({
//						    jsApiList: ['chooseImage'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
//						    success: function(res) {
//						        // 以键值对的形式返回，可用的api值true，不可用为false
//						        // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
//						        alert(JSON.stringify(res));
//						    }
//						});
						wx.chooseImage({
							count: 1, // 默认9
							sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
							sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
							success: function(res) {
//								onImageUp(res.localIds.toString(), 1);
								if(index == 1){
									onImageUp(res.localIds.toString(), 1);
									index++;
									imageId1 = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
									$("#image1").attr('src', imageId1);
									$("#openImage").css("display", "block");
									$("#image1").css("display", "inline");
								} else if (index == 2){
									onImageUp(res.localIds.toString(), 2);
									index++;
									imageId2 = res.localIds;
									$("#image2").attr('src', imageId2);
									$("#openImage").css("display", "block");
									$("#image2").css("display", "inline");
								} else if(index == 3) {
									onImageUp(res.localIds.toString(), 3);
									index++;
									imageId3 = res.localIds;
									$("#image3").attr('src', imageId3);
									$("#image3").css("display", "inline");
									$("#openImage").css("display", "none");
								}
							}
						});

					});
					
				});
			} else {
				alert("初始化失败" + data.message);
			}
		},
		error:function(e){
			$modal.modal('close');
			alert("微信JSSDK初始化失败");
		}
	});
	
	function onImageUp(path, index) {
		wx.uploadImage({
			localId: path, // 需要上传的图片的本地ID，由chooseImage接口获得
			isShowProgressTips: 0, // 默认为1，显示进度提示
			success: function(res) {
				if(index == 1) {
					imageServerId1 = res.serverId; // 返回图片的服务器端ID
				} else if(index == 2) {
					imageServerId2 = res.serverId; // 返回图片的服务器端ID
				} else {
					imageServerId3 = res.serverId; // 返回图片的服务器端ID
				}

			}
		});
	}
	
	$("#send_content").click(function(){
		var type = 1;
		var contentStr = $("#content_detail").val();
		if(contentStr.length == 0){
			new Toast({context:$('body'),message:'请输入您的观点'}).show();
			return;
		}
		var imageServerIds = "";
		if(imageServerId1 !== null && imageServerId1 !== undefined && imageServerId1 !== '') {
			imageServerIds = imageServerId1;
			if(imageServerId2 !== null && imageServerId2 !== undefined && imageServerId2 !== '') {
				imageServerIds = imageServerId1 + "," + imageServerId2;
				if(imageServerId3 !== null && imageServerId3 !== undefined && imageServerId3 !== '') {
					imageServerIds = imageServerId1 + "," + imageServerId2 + "," + imageServerId3;
				}
			}
		}
		if(imageServerIds.length == 0){
			type = 3;
		} else {
			type = 2;
		}
		$("#loading_text").text("发送中...");
		$modal.modal();
		$.ajax({
			type: "POST",
			url: urlPath,
			data: {
				token: localStorage.getItem("token"),
				g_type: type,
				details: contentStr,
				cover: imageServerIds,
				//audio : ''
			},
			dataType: "json",
			success: function(data) {
				$modal.modal('close');
				console.log(data);
				var code = data.code;
				console.log(code);
				if(code == "1") {
					new Toast({context:$('body'),message:'发送成功'}).show();
					location.href = "query_detail.html";
				} else {
					alert(data.message + "");
				}
			},
			error: function(e){
				$modal.modal('close');
				alert("服务请求异常！");
			}
		});
	});
	
});